/*
 * ConfigParser
 *
 * License: LGPL v3
 */
package org.passwordstore.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author thotheolh
 */
public class ConfigParser {

    private FileInputStream propFileInStream;
    private FileOutputStream propFileOutStream;
    private Properties prop;

    public ConfigParser() {
    }

    /**
     * Get configuration
     * @param filename
     * @return Properties
     */
    public Properties getConfig(String filename) {
        try {
            propFileInStream = new FileInputStream(filename);
            return readStream(propFileInStream);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * Get configuration
     * @param configFile
     * @return Properties
     */
    public Properties getConfig(File configFile) {
        try {
            propFileInStream = new FileInputStream(configFile);
            return readStream(propFileInStream);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * Output Configuration
     * @param filename
     * @param outProps
     */
    public void writeConfig(String filename, Properties outProps) {
        try {
            propFileOutStream = new FileOutputStream(filename);
            outProps.store(propFileOutStream, null);
        } catch (IOException ex) {
            Logger.getLogger(ConfigParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Read file input streams
     * @param stream
     * @return
     */
    public Properties readStream(FileInputStream stream) {
        try {
            prop = new Properties();
            prop.load(stream);
            return prop;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

}
